from rest_framework import filters
from rest_framework.viewsets import ModelViewSet

from apps.Info.InfoPage import InfoPagination
from apps.Info.pagination import Pagination
from apps.Info.serializers import InfoModelsSerializer, CreateInfoModelsSerializer, UpdateInfoModelsSerializer
from apps.Info.models import InfoModels
from django_filters.rest_framework import DjangoFilterBackend


class InfoModelsViewSet(ModelViewSet):
    queryset = InfoModels.objects.order_by('pk')
    serializer_class = InfoModelsSerializer
    filter_backends = (DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
    filter_fields = ("TitleInfo", "ChangeInfo", "Successfully_Info")
    search_fields = ["TitleInfo", "ChangeInfo", "Successfully_Info"]
    ordering_fields = ["create_time","Successfully_Info","ChangeInfo"]
    pagination_class = InfoPagination

    def get_serializer_class(self):
        if self.action == "create":
            return CreateInfoModelsSerializer
        elif self.action == "update":
            return UpdateInfoModelsSerializer
        else:
            return InfoModelsSerializer
